<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/8
 * Time: 16:08
 */
class RequestCdn  extends MY_Controller{
    public function __construct()
    {
        parent::__construct();
    }

  public function download(){
        try{ 
            $this->load->library('Tool');
            $this->load->helper('func');
            $this->load->database();
            $this->load->library('RedisHandle');
            $params['merCode'] = $_GET['merCode'];
            $params['time'] = $_GET['time'];
            $params['sign'] = $_GET['sign'];
            $params['vid'] =$_GET['vid'];
            $params['muid'] = $_GET['muid'];
            $param['floder'] = urldecode($_GET['floder']);
	    //var_dump($param);die();
            //必填参数验证
            mustParams($params);
            //签名验证
            $response = Tool::checkUrlSign($params);
            if($response['code']==0)
            {
                if($params['vid']==''|| $params['muid']==''){
                    json_exit(10004,'缺少参数');
                }
                $redis = RedisHandle::getInstance();
                $key = RedisHandle::getKey($params['vid']);

                $file_name = $redis->get($key);
                if($file_name==''){
                    $sql = "SELECT filename FROM file_information WHERE vid=".$params['vid'];
                    $query = $this->db->query($sql);
                    $row = $query->row();
                    $file_name = $row->filename;
                }
				
                if($param['floder']==''){
                    $url =  $this->config->item('base_url').$params['merCode'].'/'.$file_name;//没有分类自动取商户分类
					$file_path = FCPATH.'uploads/'.$params['merCode']."/".$file_name;

                }else{
                    $url = $this->config->item('base_url').$param['floder'].'/'.$file_name;
					$file_path = FCPATH.'uploads/'.$param['floder']."/".$file_name;

                }		
				
                #ob_clean();
                #if(!file_exists($file_path)){
                #    echo json_exit(40001,'文件不存在');
                #    exit;
                # }
                # $fp=fopen($file_path,"rw");
		#echo fwrite($fp);die;
                # $filesize=filesize($file_path);
		#// echo $filesize ;die;
                # header("Content-type:application/octet-stream");
                # header("Accept-Ranges:bytes");
                # header("Accept-Length:".$filesize);
                # header("Content-Disposition: attachment; filename=".basename($url));
                # $buffer=1024;
                # $buffer_count=0;
                # while(!feof($fp)&&$file_Size-$buffer_count>0){
                # $data=fread($fp,$buffer);
                # 
		#$buffer_count+=$buffer;
                #  echo $data;
                # }
                #fclose($fp);
                
		#if(file_exists($file_path)){
                #    header("Content-Type: application/force-download");
                #    header("Content-Disposition: attachment; filename=".basename($url));
                #    header("Accept-length:".filesize($file_path));
                #    readfile($file_path);
                #    exit;  
                #}else{
                #    echo json_exit(40001,'文件不存在'); 
                #}

		ob_clean();
	    if(!file_exists($file_path)){
		echo json_encode(array('code'=>40005,'msg'=>'文件不存在'));
	     exit;
	    }
	    $starttime =getMicrotime();//开始下载时间
		
	    $fp=fopen($file_path,"r");
	    $filesize=filesize($file_path);
	    header("Content-type:application/octet-stream");
	    header("Accept-Ranges:bytes");
	    header("Accept-Length:".$filesize);
	    header("Content-Disposition: attachment; filename=".basename($url));
	    $buffer=1024;
	    $buffer_count=0;
	    while(!feof($fp)&&$filesize-$buffer_count>0){
	    $data=fread($fp,$buffer);
	    flush();
	    $buffer_count+=$buffer;
	      echo  $data;
	    }
	    fclose($fp);
	     //sleep(1);
	    $endtime = getMicrotime();//结束下载时间		
	    $total =  bandWide($buffer_count , $starttime, $endtime);
	    $bandwilde = changeWide($total);
	    $downloadtimes =round(($endtime-$starttime),2);
		$downflow= getRealSize($buffer_count);
		
	    dayLog($params['merCode'],$downloadtimes,$downflow,$bandwilde);//5分钟日志
	    commonLog($params['vid'],$params['merCode'],$params['muid'],basename($url));//通用下载日志
           }else{
                echo json_exit(10004,'缺少参数');

            }
            RedisHandle::close();

         }catch(Exception $e){

        json_exit(10006,$e->getMessage());

         }      
    }
}
